package cn.dengta.webapp.user.model;

import java.math.BigDecimal;
import java.util.Arrays;
import javax.annotation.Nullable;

import cn.dengta.common.model.*;
import cn.dengta.common.util.Common;
import cn.dengta.context.model.WesKit;
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.Cmp;
import me.codeplayer.util.StringUtil;

import static cn.dengta.webapp.user.model.FeeWay.DOUBLE_SIDED;

@Getter
public enum MetaType implements ValueProxyImpl<MetaType, Integer> {

	/** 注册限制 */
	registerLimit(0, "注册限制", State.PUBLIC),
	/** 抽奖转盘 */
	lottery(1, "抽奖转盘", State.PROTECTED),
	/** 爱情树配置 */
	LOVE_TREE(2, "爱情树礼物配置", State.INTERNAL),
	/** 小鸟游戏复活所需钻石 */
	GAME_BIRD_COINS(3, "小鸟游戏复活所需钻石", State.INTERNAL),
	/** 系统配置 */
	systemConfig(4, "系统配置", State.PUBLIC),
	/** 女主播配置 */
	femaleConfig(5, "女主播配置", State.PUBLIC),
	/** 金币兑换配置 */
	gold_exchange(6, "金币兑换配置", State.PUBLIC),
	/** 用于测试环境初始化老虎机数据，方便测试 */
	TIGER_SLOT(7, "用于测试环境初始化数据，方便测试", State.PRIVATE),
	/** 圣诞节活动配置礼物id, 多个礼物逗号隔开 */
	CHRISTMAS(8, "圣诞节活动配置礼物id,多个礼物逗号隔开", State.PRIVATE),
	/** 直播配置 */
	live(9, "直播配置", State.PRIVATE),
	/** 1v1聊天/直播礼物引导 */
	GUIDE_GIFT(10, "1v1聊天/直播礼物引导", State.PRIVATE),
	/** 聊天评价配置 */
	chatEval(11, "评价配置", State.PRIVATE),
	/** 广告预警配置 */
	adWarning(12, "频繁消息触发配置", State.PRIVATE),
	/** 金钻预警配置 */
	goldWarning(13, "金钻预警配置", State.PRIVATE),
	/** 用户防骚扰 */
	disturb(14, "用户防骚扰", State.PUBLIC),
	/** 允许H5充值配置 */
	allowH5recharge(15, "H5充值配置", AllowH5recharge.class, State.PUBLIC),
	/** app首页默认tab */
	tabDefault(16, "app首页默认tab", State.PRIVATE),
	wesKitsSwitch(17, "分包配置总开关", State.PRIVATE),
	/** 国内首充配置 暂未使用保留下标 */
	interFirstPay(18, "国内首充配置", InterFirstPay.class, State.PRIVATE),
	/** 女用户免费搭讪 */
	femaleFreeGreet(19, "女用户免费搭讪", FemaleFreeGreet.class, State.PRIVATE),
	smsWxMpRechargeNum(20, "公众号短信开关", SMSWxMpRechargeNum.class, State.PUBLIC),
	feeRelationConfig(21, "男女收费模式配置", FeeRelationConfig.class, State.PUBLIC),
	/** 重复搭讪配置 */
	repeatGreet(22, "重复搭讪配置", RepeatGreetConfig.class, State.PUBLIC),

	;

	@EnumValue
	public final Integer value;
	/** type对应Value所保存的配置信息 */
	public Class<? extends TypeValue> typeValueClass;
	final ValueProxy<MetaType, Integer> proxy;

	MetaType(Integer value, String label, State state) {
		this(value, label, null, state);
	}

	// 构造函数，枚举类型只能为私有
	MetaType(Integer value, String label, Class<? extends TypeValue> typeValueClass, State state) {
		this.value = value;
		this.proxy = new ValueProxy<>(this, state, value, label);
		this.typeValueClass = typeValueClass;
	}

	public static final MetaType[] CACHE = Arrays.stream(values()).filter(m -> m.getProxy().state == State.PUBLIC).toArray(MetaType[]::new);

	public static MetaType of(@Nullable Integer value) {
		return Common.getEnum(values(), MetaType::getValue, value);
	}

	public static MetaType of(String name) {
		if (StringUtil.notEmpty(name)) {
			for (MetaType m : CACHE) {
				if (m.name().equalsIgnoreCase(name)) {
					return m;
				}
			}
		}
		return null;
	}

	public interface TypeValue {

	}

	/** 允许H5充值配置数据 */
	@Getter
	@Setter
	public static class AllowH5recharge implements TypeValue {

		/** 累计总数 */
		public RechargeConfig total;
		/** 当天 */
		public RechargeConfig currDay;

		@Getter
		@Setter
		public static class RechargeConfig {

			/** 是否启用 */
			public boolean enabled;
			/** 数值 */
			public BigDecimal value;

		}

	}

	@Getter
	@Setter
	public static class InterFirstPay implements TypeValue {

		/** 充值钻石数 */
		public Integer pc;
		/** 赠送钻石数 */
		public Integer fc;
		/** 礼物id */
		public Long gi;
		/** 礼物数量 */
		public Integer gn;
		/** 宣传图 */
		public String icon;
		/** 安卓 productId */
		public Long apId;
		/** ios productId */
		public Long ipId;

	}

	@Getter
	@Setter
	public static class FemaleFreeGreet implements TypeValue {

		/** 是否打开 */
		private Boolean enabled = Boolean.FALSE;
		/** 每日免费搭讪次数 */
		private Integer dayFreeNum = 0;
		/** 推广员女用户每日次数 */
		private Integer agentDayNum = 0;
		/** 每小时可搭讪次数 */
		private Integer hourNum = 0;
		/** 是否拦截 */
		private Boolean intercept = Boolean.FALSE;
		/** 当 intercept=false时不拦截群体：0=全部；1=普通女用户；2=推广员女用户 */
		private Integer inType = 0;
		/** 女神活动赠送免费搭讪次数， 不受开关影响 */
		private Integer goddessFreeNum = 0;

		public boolean validateIntercept(Boolean hasAgent) {
			if (intercept) {
				return true;
			}
			if (Cmp.eq(inType, 0)) {
				return false;
			}
			return (hasAgent && Cmp.eq(inType, 2)) || (!hasAgent && Cmp.eq(inType, 1));
		}

	}

	@Getter
	@Setter
	public static class SMSWxMpRechargeNum implements TypeValue {

		/** 是否打开 */
		private Boolean enabled = Boolean.FALSE;

		private Integer num = null;

	}

	@Getter
	@Setter
	public static class FeeRelationConfig implements TypeValue {

		private Integer cn = DOUBLE_SIDED.getValue();
		private Integer cnWomanToIntlMan = FeeWay.MAN.getValue();
		private Integer intl = FeeWay.MAN.getValue();
		private Integer intlWomanToCnMan = DOUBLE_SIDED.getValue();

		public FeeWay calc(WesKit fromWs, Gender fromGd, WesKit toWs, Gender toGd) {
			if (fromGd == toGd) {
				return DOUBLE_SIDED;
			}
			WesKit womanWs = fromGd == Gender.MALE ? toWs : fromWs;
			WesKit manWs = fromGd == Gender.MALE ? fromWs : toWs;
			if (womanWs.isInterior() && manWs.isInterior()) {
				return FeeWay.of(cn);
			}
			if (!womanWs.isInterior() && !manWs.isInterior()) {
				return FeeWay.of(intl);
			}
			if (womanWs.isInterior()) {
				return FeeWay.of(cnWomanToIntlMan);
			}
			return FeeWay.of(intlWomanToCnMan);
		}

	}

	@Getter
	@Setter
	public static class RepeatGreetConfig implements TypeValue {

		/** 是否打开 */
		private Boolean enabled = Boolean.FALSE;
		/** 最近天数 */
		private Integer recentDays = 5;
		/** 回复数 */
		private Integer replyNum = 10;

	}

}
